package org.example.terminal;

import org.example.dao.EmployeeDAO;
import org.example.dao.SalaryDAO;
import org.example.service.DepartmentService;
import org.example.service.EmployeeService;
import org.example.service.SalaryService;
import org.example.utils.DataUtils;
import org.example.utils.TableUtils;
import org.example.utils.TimeUtils;

import java.util.ArrayList;
import java.util.Scanner;

public class SalaryMenu {
    public static void salaryMenu() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("==================================");
        System.out.println("==========薪资数据管理==============");
        System.out.println("===1、薪资新增======================");
        System.out.println("===2、薪资修改======================");
        System.out.println("===3、薪资删除======================");
        System.out.println("===4、薪资查询======================");
        System.out.println("===5、退出模块======================");
        System.out.println("==================================");
        System.out.println("请输入要执行的功能序号：");

        int operation = 0;
        try {
            operation = scanner.nextInt();
            switch (operation) {
                case 1:
                    add(scanner);
                    salaryMenu();
                    break;
                case 2:
                    update(scanner);
                    salaryMenu();
                    break;
                case 3:
                    delete(scanner);
                    salaryMenu();
                    break;
                case 4:
                    query(scanner);
                    salaryMenu();
                    break;
                case 5:
                    System.out.println("已退出薪资管理！");
                    break;
                default:
                    System.out.println("您输入的功能序号不存在，请检查功能菜单并重新输入！");
                    salaryMenu();
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            salaryMenu();
        }
    }

    private static void query(Scanner scanner) throws Exception {
        System.out.println("=========薪资数据查询=========");
        System.out.println("===1查询某员工薪资历史明细======");
        System.out.println("===2查询某年月薪资历史明细======");

        int i = scanner.nextInt();
        //如果i不等于1或者不等于2
        if (i != 1 && i != 2) {
            throw new Exception("输入的查询编号错误，请重新输入");
        }

        if (i == 1){
            ArrayList<String[]> data = EmployeeDAO.getEmployeeInfo();
            if (SalaryService.checkEmpEmpty()) {
                throw new Exception("===当前员工数据为空，请先添加员工数据===");
            }

            boolean empData = EmployeeMenu.query();
            if (!empData) {
                return;
            }

            System.out.println("====请输入一个要查询的员工序号====");
            int num = scanner.nextInt();
            //序号范围校验
            if (!SalaryService.checkNumIsLegal(num, data)) {
                throw new Exception("请正确输入员工对应的序号");
            }

            //数据回显
            String empId = "";
            String empName = "";
            String currentMonth = TimeUtils.getSalaryDate();
            if (data != null) {
                empId = data.get(num - 1)[0];
                empName = data.get(num - 1)[4];
            }
            System.out.println("=====员工[" + empName + "]薪资历史信息======");


            //service查询
            ArrayList<String[]> resSalaryData = SalaryService.querySalaryByEmpId(empId);

            if (resSalaryData != null && resSalaryData.isEmpty()) {
                throw new Exception("======当前员工薪资数据为空======");
            }

            if (resSalaryData != null) {
                String[] header = {"序号", "保险扣除", "实发薪资", "员工姓名", "薪资月份", "绩效工资", "基础薪资"};
                TableUtils.printTable(header, DataUtils.listToStringArray(resSalaryData));
            } else {
                throw new Exception("错误！请联系技术人员");
            }
        }else {
            System.out.println("请按照标准格式输入想要查询的年月（例：2021-03）");
            String saDate = "";
            try {
                saDate = scanner.next();
            } catch (Exception e) {
                throw new Exception("输入不合法");
            }
            if (!saDate.matches("[1-9]?\\d{3}-(10|11|12|0[1-9])")) {
                throw new Exception("输入的薪资查询起始时间格式错误，请重新输入");
            }

            ArrayList<String[]> data = EmployeeService.getEmployeeInfo();
            if (data != null && data.isEmpty()) {
                throw new Exception("======当前薪资数据为空======");
            }

//            boolean empData = EmployeeMenu.query();
//            if (!empData) {
//                return;
//            }

            System.out.println("====="+saDate+"员工薪资信息====");

            //service查询
            ArrayList<String[]> resSalaryData = SalaryService.querySalaryBySaDate(saDate);
            if (resSalaryData == null ||resSalaryData.isEmpty()) {
                throw new Exception("======当前薪资数据为空======");
            }

            String[] header = {"序号", "保险扣除", "实发薪资", "员工姓名", "薪资月份", "绩效工资", "基础薪资"};
            TableUtils.printTable(header, DataUtils.listToStringArray(resSalaryData));
        }
    }

    private static void delete(Scanner scanner) throws Exception {
        System.out.println("========薪资数据删除=========");
        boolean empData = EmployeeMenu.query();
        if (!empData) {
            return;
        }
        EmployeeMenu.query();
        System.out.println("======请输入要删除的员工序号======");
        int num = 0;
        try {
            num = scanner.nextInt();
        } catch (Exception e) {
            throw new Exception("输入不合法");
        }
        ArrayList<String[]> data = EmployeeDAO.getEmployeeInfo();
        //序号范围校验
        if (!SalaryService.checkNumIsLegal(num, data)) {
            throw new Exception("请正确输入员工对应的序号");
        }


        String empId = "";
        String currentMonth = TimeUtils.getSalaryDate();
        if (data != null) {
            empId = data.get(num - 1)[0];
        }

        //检测是否有员工对应薪资信息
        if (!SalaryDAO.checkCurrentMonthSalary(currentMonth, empId)) {
            System.out.println("=====该员工当月薪资没有录入，请先录入薪资=====");
            return;
        }

        SalaryService.deleteSalary(empId,currentMonth);
    }

    private static void update(Scanner scanner) throws Exception {
        System.out.println("==================================");
        System.out.println("==========薪资数据修改==============");


        if (SalaryService.checkEmpEmpty()) {
            throw new Exception("===当前员工数据为空，请先添加员工数据===");
        }
        EmployeeMenu.query();

        ArrayList<String[]> data = EmployeeService.getEmployeeInfo();
        if (data != null) {
            String[] header = {"序号", "员工性别", "员工工号", "部门名称", "员工名称"};
            System.out.println("========员工数据查询========");
            TableUtils.printTable(header, DataUtils.listToStringArray(data));
        } else {
            throw new Exception("错误！请联系技术人员");
        }

        System.out.println("======请输入修改的员工序号======");

        int num = 0;
        try {
            num = scanner.nextInt();
        } catch (Exception e) {
            throw new Exception("输入格式错误");
        }
        //序号范围校验
        if (!SalaryService.checkNumIsLegal(num, data)) {
            throw new Exception("请正确输入员工对应的序号");
        }
        String[] oldEmpSalaryInfo = SalaryService.getOldEmpSalaryInfo(data, num);
        if (!SalaryDAO.checkCurrentMonthSalary(oldEmpSalaryInfo[4], oldEmpSalaryInfo[0])) {
            throw new Exception("=====该员工当月薪资没有录入，请先录入薪资=====");
        }
        System.out.println("=====请输入基础薪资，原数据为：" + oldEmpSalaryInfo[1] + "======");
        double newSaBase = scanner.nextDouble();
        System.out.println("=====请输入绩效薪资，原数据为：" + oldEmpSalaryInfo[2] + "======");
        double newSaPerformance = scanner.nextDouble();
        System.out.println("=====请输入保险扣除薪资，原数据为：" + oldEmpSalaryInfo[3] + "======");
        double newSaInsurance = scanner.nextDouble();

        //service修改
        SalaryService.updateSalary(newSaBase, newSaPerformance, newSaInsurance, oldEmpSalaryInfo[0], oldEmpSalaryInfo[4]);
    }


    public static void add(Scanner scanner) throws Exception {
        System.out.println("==================================");
        System.out.println("==========薪资数据新增==============");
        if (SalaryService.checkEmpEmpty()) {
            throw new Exception("===当前员工数据为空，请先添加员工数据===");
        }
        EmployeeMenu.query();
        System.out.println("======请选择一个员工进行薪资数据添加======");
        int num = 0;
        try {
            num = scanner.nextInt();
        } catch (Exception e) {
            System.out.println("输入不合法，请重新输入");
        }
        ArrayList<String[]> employeeInfo = EmployeeDAO.getEmployeeInfo();
        //序号范围校验
        if (!SalaryService.checkNumIsLegal(num, employeeInfo)) {
            throw new Exception("请正确输入员工对应的序号");
        }

        String saDate = TimeUtils.getSalaryDate();
        //已选择员工数据回显
        String empName = "";
        String empId = "";
        if (employeeInfo != null) {
            empName = employeeInfo.get(num - 1)[4];
            empId = employeeInfo.get(num - 1)[0];
        } else {
            throw new Exception("员工数据为空！");
        }
        System.out.println("=======当前操作员工[" + empName + "]员工的[" + saDate + "]薪资信息=====");

        //检查当月员工薪资是否已添加
        if (SalaryDAO.checkCurrentMonthSalary(saDate, empId)) {
            throw new Exception("====该员工当月薪资已添加，数据添加失败=====");
        }
        System.out.println("======请输入基础薪资======");
        double saBase = scanner.nextDouble();
        System.out.println("======请输入绩效薪资======");
        double saPerformance = scanner.nextDouble();
        System.out.println("======请输入保险扣除薪资======");
        double saInsurance = scanner.nextDouble();

        //service新增
        SalaryService.addSalary(saBase, saPerformance, saInsurance, empId, saDate);
    }


}
